package com.sc.VenusQueryBeta.ui.activites.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.hengjun.kingstar.sdk.MessageEvent3;
import com.hengjun.kingstar.sdk.base.BasePresenter;
import com.hengjun.kingstar.sdk.base.activity.BaseMVPCompatActivity;
import com.hengjun.kingstar.sdk.rxbus.RxBus;
import com.hengjun.kingstar.sdk.utils.LogUtils;
import com.hengjun.kingstar.sdk.utils.SpUtils;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.constant.Constants;
import com.sc.VenusQueryBeta.contract.setting.change.ChangeContract;
import com.sc.VenusQueryBeta.global.MyApplication;
import com.sc.VenusQueryBeta.model.bean.home.ChangeNextBean;
import com.sc.VenusQueryBeta.model.bean.login.UsersLoginBean;
import com.sc.VenusQueryBeta.presenter.setting.change.ChangePresenter;
import com.sc.VenusQueryBeta.ui.activites.MainActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangePasswordActivity extends BaseMVPCompatActivity<ChangeContract.ChangePresenter> implements ChangeContract.IChangeView {


    @BindView(R.id.about_register_back)
    ImageButton changePasswordBack;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.about_kingstar_infornation)
    AppBarLayout aboutKingstarInfornation;
    @BindView(R.id.new_password)
    EditText newPassword;
    @BindView(R.id.comfirm_new_password)
    EditText comfirmNewPassword;
    @BindView(R.id.old_password)
    EditText oldPassword;
    @BindView(R.id.btn_sure)
    Button btnSure;

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_password;
    }


    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return ChangePresenter.newInstance();
    }

    /**
     * 得到后台返回数据成功
     *
     * @param changeNextBean
     */
    @Override
    public void showChangeNextSuccess(ChangeNextBean changeNextBean) {
        switch (changeNextBean.getReturnCod()) {
            case 400:  //用户不存在
                hideProgressDialog();
                ToastUtils.showToast("用户不存在");
                break;
            case 401:  //密码错误
                hideProgressDialog();
                ToastUtils.showToast("密码错误");
            case 402: //修改成功
                hideProgressDialog();
                SpUtils.clear(MyApplication.app);
                ToastUtils.showToast("修改成功");
//                startWithPop(VipRootFragment.newInstance());
                finish();
//                SpUtils.clear(MyApplication.app);
                SpUtils.putBoolean(MyApplication.app, Constants.ISCHANGEPWD, true);
              RxBus.getInstance().post(new MessageEvent3("123"));//发送登录开始计时消息  MyApplication 有接受
//                Intent intent = new Intent(this, MainActivity.class);
////              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);
//                finish();
                break;
        }
    }

    @Override
    public void showNetworkError() {
        hideProgressDialog();
        ToastUtils.showToast("修改失败");
    }




    @OnClick({R.id.about_register_back, R.id.btn_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.about_register_back:
                finish();
                break;
            case R.id.btn_sure:
                UsersLoginBean loginBean = SpUtils.getObject(MyApplication.app, UsersLoginBean.class);
                LogUtils.e("loginbean="+loginBean);
                if (!newPassword.getText().toString().trim().equals("") && !comfirmNewPassword.getText().toString().trim().equals("")) {
                    if (newPassword.getText().toString().equals(comfirmNewPassword.getText().toString())) {
                        if (oldPassword.getText().toString().trim().equals(loginBean.getPassword())) {
                            showProgressDialog("正在修改");
                            if (loginBean != null) {
                                mPresenter.changeNext(oldPassword.getText().toString().trim(), newPassword.getText().toString().trim(), loginBean.getRegisterIndex(), loginBean.getId());
                            }
                        }else {
                            ToastUtils.showToast("舊密碼不一致");
                        }
                    } else {
                        ToastUtils.showToast("两次输入的新密码不一致");
                    }
                } else {
                    ToastUtils.showToast("密码不能为空");
                }
                break;
        }
    }
}
